<template>
	<view class="address-edit">
		<view class="form bg-white">
			<u-field v-model="addressObj.contact" label="收货人" placeholder="请填写收货人姓名">
			</u-field>
			<u-field v-model="addressObj.telephone" label="联系方式" placeholder="请填写手机号码">
			</u-field>
			<view @click="showRegion = true">
				<u-field v-model="region" :disabled="true" label="所在地区" placeholder="请选择省、市、区" right-icon="arrow-right">
				</u-field>
			</view>
			<view>
				<u-field v-model="addressObj.address" type="textarea" label="详细地址" placeholder="请填写小区、街道、门牌号等信息"
					:field-style="{ flex: 1, height: '200rpx' }" />
			</view>
			<view @click="showSchool=true">
				<u-field v-model="region" :disabled="true" label="收货学校" placeholder="请选择收货学校" right-icon="arrow-right">
				</u-field>
			</view>
		</view>
		<view class="m-t-10 m-b-10 bg-white p-20">
			<u-checkbox @click="changeDefault" v-model="addressObj.is_default" shape="circle">
				<text class="xs">默认地址</text>
			</u-checkbox>
		</view>
		<view class="footer flex row-between fixed">
			<view class="btn bg-primary white md flex row-center" @tap="formSubmit">确认</view>
		</view>
		<u-select v-model="showRegion" mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select>
		<school-popup :show="showSchool" @close="showSchool = false" @confirm="schoolConfirm"></school-popup>
	</view>
</template>

<script>
	import {
		editAddress,
		getOneAddress,
		hasRegionCode,
		addAddress,
	} from "@/api/user";
	import area from "@/utils/area";
	export default {
		data() {
			return {
				addressObj: {
					contact: "",
					telephone: "",
					province: "",
					city: "",
					district: "",
					address: "",
					is_default: false,
				},
				region: "",
				addressId: "",
				defaultRegion: ["广东省", "广州市", "番禺区"],
				defaultRegionCode: "440113",
				showRegion: false,
				lists: [],
				showSchool:false,
				schoolItem:null
			};
		},
		onLoad: function(options) {
			this.addressId = parseInt(options.id);
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑地址",
				});
				this.getOneAddressFun();
			} else {
				uni.setNavigationBarTitle({
					title: "添加地址",
				});
				this.getWxAddressFun();
			}
			this.$nextTick(() => {
				this.lists = area;
			});
		},

		onUnload: function() {
			uni.removeStorageSync("wxAddress");
		},

		methods: {
			async formSubmit() {
				let {
					addressObj: {
						contact,
						telephone,
						province_id,
						city_id,
						district_id,
						is_default,
						address,
					},
					addressId,
					region,
				} = this;
				if (!contact)
					return this.$toast({
						title: "请填写收货人姓名",
					});
				if (!telephone)
					return this.$toast({
						title: "请填写手机号码",
					});
				if (!region)
					return this.$toast({
						title: "请选择省、市、区",
					});
				if (!address)
					return this.$toast({
						title: "请填写小区、街道、门牌号等信息",
					});
				const params = {
					contact,
					telephone,
					province_id: parseInt(province_id),
					city_id: parseInt(city_id),
					district_id: parseInt(district_id),
					is_default: is_default ? 1 : 0,
					id: addressId,
					address,
				};
				const {
					code,
					msg
				} = addressId
					?
					await editAddress(params) :
					await addAddress(params);
				if (code == 1) {
					this.$toast({
						title: msg,
					}, {
						tab: 3,
						url: 1,
					});
				}
			},
			regionChange(region) {
				this.addressObj.province_id = region[0].value;
				this.addressObj.city_id = region[1].value;
				this.addressObj.district_id = region[2].value;
				this.region =
					region[0].label + " " + region[1].label + " " + region[2].label;
			},

			getOneAddressFun() {
				getOneAddress(this.addressId).then((res) => {
					if (res.code == 1) {
						let {
							city,
							province,
							district
						} = res.data;
						this.addressObj = res.data;
						this.region = `${province} ${city} ${district}`;
					}
				});
			},

			getWxAddressFun() {
				let wxAddress = uni.getStorageSync("wxAddress");
				if (!wxAddress) return;
				wxAddress = JSON.parse(wxAddress);
				let {
					userName: contact,
					telNumber: telephone,
					provinceName: province,
					cityName: city,
					detailInfo: address,
				} = wxAddress;
				let district = wxAddress.countryName || wxAddress.countyName;
				hasRegionCode({
					province,
					city,
					district,
				}).then((res) => {
					if (res.code == 1) {
						if (res.data.province && res.data.city && res.data.district) {
							this.region = `${province} ${city} ${district}`;
							this.addressObj.province_id = res.data.province;
							this.addressObj.city_id = res.data.city;
							this.addressObj.district_id = res.data.district;
						}
						this.addressObj.contact = contact;
						this.addressObj.telephone = telephone;
						this.addressObj.address = address;
					}
				});
			},
			schoolConfirm(item){
				this.schoolItem=item
			}
		},
	};
</script>
<style lang="scss">
	.address-edit {
		padding-top: 10rpx;

		.footer {
			position: fixed;
			display: flex;
			width: 100%;
			height: 154rpx;
			bottom: 0;
			padding: 30rpx 20rpx;
			justify-content: center;
			text-align: center;
			.btn {
				position: relative;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 378rpx;
				height: 75rpx;
				font-size: 32rpx;
				border-radius: 10px;
				background: $-color-primary;
				color: #ffffff;
				letter-spacing: 20rpx;
			}
		}
	}
</style>